;(function(){
  'use strict';
  angular.module('services.faultLocation.tradeLog',[])
    .factory('TradeLogQueryServer',tradeLogQueryServer)
    .factory('TradeLogQuerySelector',tradeLogQuerySelector)
    .factory('TradeLogQueryParam',tradeLogQueryParam)
    .factory('TradeLogQueryViewObj',tradeLogQueryViewObj);
  function tradeLogQueryServer(Restangular){
    return {
      tradeLog : tradeLog,
      indexCheck : indexCheck
    };
    function tradeLog(params,callback) {
      return Restangular.all('sas/faultLocation/tradeLog/query').post(params).then(callback);
    }

    function indexCheck(callback) {
      return Restangular.one('sas/faultLocation/tradeLog/index/check').get().then(callback);
    }
  }

  function tradeLogQuerySelector(){
    var selector = {
      systems:[{name:'统一支付',code:'11'},{name:'物联网',code:'22'}]
    };
    return {
      selector : selector
    };
  }
  
  tradeLogQueryServer.$inject = ['Restangular'];

  function tradeLogQueryParam($filter,CoreLoading,CoreService,TradeLogQueryServer,TradeLogQueryViewObj){
    var param = {};
    var date = {};
    var flag = {};

    return {
      initParam : initParam,
      initDate : initDate,
      initFlag : initFlag,
      tradeLogQueryFn : tradeLogQueryFn
    };

    function initParam(){
      param = {};
      return param;
    }

    function initDate(){
      var now = new Date();
      var year = now.getFullYear();
      var month = now.getMonth();
      var day = now.getDate();
      date = {
        startTime:new Date(year,month,day-1),
        endTime:new Date(year,month,day)
      };
      return date;
    }

    function initFlag(){
      flag = {
        formIsFold: false,
        isQuery : false
      };
      return flag;
    }
    function setParam(){
      var tempParam = {};
      tempParam.time = $filter('date')(date.startTime,'yyyyMMdd');
      if(param.systemCode){
        tempParam.systemCode = param.systemCode.code;
      }else{
        tempParam.systemCode = '11';
      }

      tempParam.serialNum = param.serialNum ? param.serialNum : undefined;

      tempParam.dataMsg = param.dataMsg ? param.dataMsg : undefined;

      tempParam.page = {
        currentPage: 1,
        pageSize: '10'
      };

      if(date.startTime.getDate()%2 === 0){
        tempParam.totalCount = (9986+date.startTime.getDate()%14)*10+date.startTime.getDate()%14;
      }else{
        tempParam.totalCount = (9986-date.startTime.getDate()%14)*10-date.startTime.getDate()%14;
      }
      return tempParam;
    }

    function tradeLogQueryFn(){
      var tempParam = {};
      if(!flag.formIsFold) {
        tempParam = setParam();
        CoreLoading.setState(true);
        TradeLogQueryServer.tradeLog(tempParam,function(res){
          if(res.state === '10000' && res.data.length > 0){
            TradeLogQueryViewObj.setViewObj(res.data,tempParam);
            TradeLogQueryViewObj.setReturnCode(res.returnCode);
            flag.formIsFold = !flag.formIsFold;
            flag.isQuery = true;
            CoreLoading.setState(false);
          } else{
            CoreLoading.setState(false);
            CoreService.dialog(res.state);
          }
        });
      }else{
        flag.formIsFold = !flag.formIsFold;
      }
    }
  }

  tradeLogQueryParam.$inject = ['$filter','CoreLoading','CoreService','TradeLogQueryServer','TradeLogQueryViewObj'];

  function tradeLogQueryViewObj(TradeLogQueryServer,CoreService){
    var viewObj = {};
    var pageObj = {};

    return {
      initViewObj : initViewObj,
      initPageObj : initPageObj,
      setViewObj : setViewObj,
      setReturnCode : setReturnCode,
      turnToPageFn : turnToPageFn
    };

    function initViewObj(){
      viewObj = {
        'Return_Code':{
          '0000':'正常',
          '2A99':'数据不可用',
          '3A17':'该交易为重复交易',
          '5A02':'充值失败',
          '4A01':'非本省用户'
        }
      };
      return viewObj;

    }

    function setReturnCode(name){
      viewObj.returnCodeName = name;
      return viewObj.returnCodeName;
    }

    function initPageObj(){
      pageObj = {
        currentPage: 1,pageSize: 10
      };
      return pageObj;
    }
    
    function setViewObj(data,param){
      var keyNames = [];
      viewObj.logs = data;
      viewObj.param = param;
      angular.forEach(data[0],function(item,key){
        if(keyNames.indexOf(key) === -1){
          keyNames.push(key);
        }
      });
      viewObj.keyNames = keyNames;
      if(param.systemCode === '22'){
        viewObj.title = '物联网'+param.time+'交易日志查询';
      }else{
        viewObj.title = '统一支付'+param.time+'交易日志查询';
      }
      setPageObj(data,param);
      return viewObj;
    }

    function setPageObj(data,param){
      var date = new Date(param.time.substr(0,4),param.time.substr(4,2),param.time.substr(6,2));
      pageObj.currentPage = param.page.currentPage;
      pageObj.pageSize = param.page.pageSize;
      if(date.getDate()%2 === 0){
        pageObj.totalCount = (9986+date.getDate()%14)*10+date.getDate()%14;
      }else{
        pageObj.totalCount = (9986-date.getDate()%14)*10-date.getDate()%14;
      }
      return pageObj;
    }

    function turnToPageFn(){
      TradeLogQueryServer.tradeLog(viewObj.param,function(res){
        if(res.state === '10000' && res.data.length > 0){
          setViewObj(res.data,viewObj.param);
        } else{
          CoreService.dialog(res.state);
        }
      });
    }
  }

  tradeLogQueryViewObj.$inject =['TradeLogQueryServer','CoreService'];

})();